package com.sky.aspect;

import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.time.LocalDateTime;
@Component
@Aspect
public class UpdateAspect {
    @Around("@annotation(com.sky.annotation.UpdateAnnotation)")
    public Object enhanceUpdate (ProceedingJoinPoint pjp) throws Throwable {
        Object[] args = pjp.getArgs();
        Object obj = args[0];
        Class<?> clazz = obj.getClass();
        Field field = clazz.getDeclaredField("updateTime");
        field.setAccessible(true);
        field.set(obj, LocalDateTime.now());
        Field field1 = clazz.getDeclaredField("updateUser");
        field1.setAccessible(true);
        field1.set(obj, BaseContext.getCurrentId());
        Field field2 = clazz.getDeclaredField("status");
        field2.setAccessible(true);
        field2.set(obj, StatusConstant.ENABLE);
        Object proceed = pjp.proceed();
        return proceed;
    }
}
